package com.uchengwang.ui.cm_ui.me.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;

/**
 * Created by U城网 on 2017/8/14.
 * 关注我们/帮助
 */

public class LoadWebActivity extends BaseWithTitleActivity{
    private WebView app_web;
    private int type;//1帮助 2 关于我们

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_app_update);
        type = getIntent().getIntExtra(BaseConfig.TYPE, 0);

        initView();
    }

    private void initView(){
        app_web = (WebView) findViewById(R.id.app_web);

        app_web.getSettings().setDefaultTextEncodingName("utf-8");
        app_web.getSettings().setJavaScriptEnabled(true);
        app_web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        app_web.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

        switch (type){
            case 1:
                //帮助
                app_web.loadUrl(BaseConfig.BaseURL+"/help.html#/");
                setTitle("帮助");
                break;
            case 2://关于我们
                app_web.loadUrl(BaseConfig.BaseURL+"/about.html#/");
                setTitle("关于我们");
                break;
        }
        app_web.setWebChromeClient(new WebChromeClient());

        app_web.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String s, String s1, String s2, String s3, long l) {
                Uri uri = Uri.parse(s);
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);

                LoadWebActivity.this.finish();
            }
        });

        //打开系统浏览器
        app_web.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (app_web != null){
            app_web.onPause();
            app_web.destroy();
        }

    }
}
